package fy.uap.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.util.DBUtil;

public class CompanyTypeDao {
	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(CompanyTypeDao.class);
	private List<String[]> companyTypeList = new ArrayList<String[]>();

	private static CompanyTypeDao companyTypeDao;

	private CompanyTypeDao() {
		initCompanyTypeList();
	}

	public static CompanyTypeDao getInstance() {
		if (companyTypeDao == null) {
			synchronized (CompanyTypeDao.class) {
				if (companyTypeDao == null)
					companyTypeDao = new CompanyTypeDao();
			}
		}
		if (companyTypeDao.companyTypeList.size() == 0) {
			synchronized (CompanyTypeDao.class) {
				if (companyTypeDao.companyTypeList.size() == 0)
					companyTypeDao.initCompanyTypeList();
			}
		}
		return companyTypeDao;
	}

	public List<String[]> getCompanyTypeList() {
		return companyTypeList;
	}

	// public static void setCompanyTypeList(List<String[]> companyTypeList) {
	// CompanyTypeDao.companyTypeList = companyTypeList;
	// }

	public String getCompanyTypeID(String typeName) {
		if (companyTypeList != null && typeName != null) {
			for (int i = 0, size = companyTypeList.size(); i < size; i++) {
				String[] array = companyTypeList.get(i);
				if (array[1].equals(typeName)) {
					return array[0];
				}
			}
		}
		return null;
	}

	private void initCompanyTypeList() {
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());
			log.info("查询公司类型");
			stat = conn.createStatement();
			rs = stat
					.executeQuery("select pk_defdoc, docname from bd_defdoc where (pk_defdoclist = '0001ORGCORP000000002' and (pk_corp = '0001' or pk_corp = '0001')) and (sealflag is null or sealflag <> 'Y') and (pk_defdoc1 is null) order by doccode");
			while (rs.next()) {
				String[] array = { rs.getString(1), rs.getString(2) };
				companyTypeList.add(array);
			}
			log.info("存储公司类型");
		} catch (Exception e) {
			log.error("异常", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, stat, conn);
		}
		log.info("结束");
		System.out.println("获取公司类型成功");
	}
}
